<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小说名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入小说名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类id" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小说状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择小说状态" clearable>
          <el-option
            v-for="dict in dict.type.novel_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字数" prop="wordCount">
        <el-input
          v-model="queryParams.wordCount"
          placeholder="请输入字数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阅读量" prop="viewCount">
        <el-input
          v-model="queryParams.viewCount"
          placeholder="请输入阅读量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="喜欢人数" prop="likeCount">
        <el-input
          v-model="queryParams.likeCount"
          placeholder="请输入喜欢人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论数" prop="commentCount">
        <el-input
          v-model="queryParams.commentCount"
          placeholder="请输入评论数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分" prop="collectCount">
        <el-input
          v-model="queryParams.collectCount"
          placeholder="请输入积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者id" prop="authorId">
        <el-input
          v-model="queryParams.authorId"
          placeholder="请输入作者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="笔名" prop="penName">
        <el-input
          v-model="queryParams.penName"
          placeholder="请输入笔名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['novel:novels:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['novel:novels:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['novel:novels:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['novel:novels:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="novelsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小说id" align="center" prop="novelId" />
      <el-table-column label="小说名称" align="center" prop="title" />
      <el-table-column label="小说封面" align="center" prop="coverImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="description" />
      <el-table-column label="分类id" align="center" prop="categoryId" />
      <el-table-column label="小说状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.novel_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="字数" align="center" prop="wordCount" />
      <el-table-column label="阅读量" align="center" prop="viewCount" />
      <el-table-column label="喜欢人数" align="center" prop="likeCount" />
      <el-table-column label="评论数" align="center" prop="commentCount" />
      <el-table-column label="积分" align="center" prop="collectCount" />
      <el-table-column label="作者id" align="center" prop="authorId" />
      <el-table-column label="笔名" align="center" prop="penName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['novel:novels:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['novel:novels:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改小说对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小说名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入小说名称" />
        </el-form-item>
        <el-form-item label="小说封面" prop="coverImage">
          <image-upload v-model="form.coverImage"/>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="小说状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.novel_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字数" prop="wordCount">
          <el-input v-model="form.wordCount" placeholder="请输入字数" />
        </el-form-item>
        <el-form-item label="阅读量" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入阅读量" />
        </el-form-item>
        <el-form-item label="喜欢人数" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入喜欢人数" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="积分" prop="collectCount">
          <el-input v-model="form.collectCount" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="作者id" prop="authorId">
          <el-input v-model="form.authorId" placeholder="请输入作者id" />
        </el-form-item>
        <el-form-item label="笔名" prop="penName">
          <el-input v-model="form.penName" placeholder="请输入笔名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNovels, getNovels, delNovels, addNovels, updateNovels } from "@/api/novel/novels"

export default {
  name: "Novels",
  dicts: ['novel_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小说表格数据
      novelsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        coverImage: null,
        description: null,
        categoryId: null,
        status: null,
        wordCount: null,
        viewCount: null,
        likeCount: null,
        commentCount: null,
        collectCount: null,
        authorId: null,
        penName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "小说名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类id不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询小说列表 */
    getList() {
      this.loading = true
      listNovels(this.queryParams).then(response => {
        this.novelsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        novelId: null,
        title: null,
        coverImage: null,
        description: null,
        categoryId: null,
        status: null,
        wordCount: null,
        viewCount: null,
        likeCount: null,
        commentCount: null,
        collectCount: null,
        authorId: null,
        penName: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.novelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加小说"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const novelId = row.novelId || this.ids
      getNovels(novelId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改小说"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.novelId != null) {
            updateNovels(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addNovels(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const novelIds = row.novelId || this.ids
      this.$modal.confirm('是否确认删除小说编号为"' + novelIds + '"的数据项？').then(function() {
        return delNovels(novelIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('novel/novels/export', {
        ...this.queryParams
      }, `novels_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
